package com.jianganming.memento;

import java.util.Stack;

/**
 * 备忘录
 */
public class MementoPattern {

    public static void main(String[] args) {
        Caretaker caretaker = new Caretaker();

        Originator originator = new Originator("1","江安明","A");
        Memento a = originator.create();
        caretaker.addMemento(a);//备份数据
        System.out.println("===========原始数据============");
        System.out.println(a);//原始数据

        originator.setStatus("B");//更新原始数据
        originator.setName("刘德华");
        Memento b = originator.create();
        caretaker.addMemento(b);//备份数据
        System.out.println("===========首次更新后的数据============");
        System.out.println(b);

        originator.setStatus("C");//更新原始数据
        originator.setName("张学友");
        Memento c = originator.create();
        System.out.println("===========第二次更新后的数据============");
        System.out.println(c);



        Memento d = caretaker.getMemento();//将原始数据赋值给originator对象
        System.out.println("===========还原首次更新后数据============");
        System.out.println(d);

        Memento e = caretaker.getMemento();//将原始数据赋值给originator对象
        System.out.println("===========还原原始数据============");
        System.out.println(e);


    }
}

class Memento{
    private String id;
    private String name;
    private String status;

    public Memento(String id, String name, String status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    @Override
    public String toString() {
        return "Memento{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", status='" + status + '\'' +
                '}';
    }
}

class Originator{
    private String id;
    private String name;
    private String status;

    public Originator(String id, String name, String status) {
        this.id = id;
        this.name = name;
        this.status = status;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }


    //构建原始数据
    public Memento create() {
        return new Memento(id,name,status);
    }

}

//备份历史数据
class Caretaker{
    private Stack<Memento> stack = new Stack<>();

    public Memento getMemento() {
        return stack.pop();
    }

    public void addMemento(Memento memento) {
        stack.push(memento);
    }
}
